Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Field::Select more flexible #1220

Closed
wants to merge 7 commits into from
Closed

Conversation

jumjamjohn
Copy link

@jumjamjohn jumjamjohn commented Sep 25, 2018

  • This PR adds more flexibility to Field::Select so we can set an array of arrays and a hash to collection option. This behavior would be more intuitive since it follows Rails form helper convention.
  • This also solves the absence of Enum field referred to Add search support for ActiveRecord::Enum columns #322 and Enum fields #533 so we don't have to create a Enum field as a custom or built-in field anymore.
  • Added select_spec.rb so we can test Select field

By default, it displays label data(e.g. "Pending") instead of raw persisted data(e.g. 1) on show/index page. Search feature would be an issue because users would want to search by label data but the system searches only by persisted data for the moment. However, that issue should be solved by another PR, not this one. Thank you for this awesome gem 😃

Basic usage:

status: Field::Select.with_options(collection: [ ['Pending',1], ['Approved',2], ['Rejected',3] ])

=>

<select name="post[status]" id="post_status">
  <option selected="selected" value="1">Pending</option>
  <option value="2">Approved</option>
  <option value="3">Rejected</option>
</select>

For Enum:

status: Field::Select.with_options(collection: Post.localized_statuses.invert)
class Post < ApplicationRecord
  enum status: { submitted: 0, approved: 1, rejected: 2 }
  def self.localized_statuses
    I18n.t 'activerecord.attributes.post/status'
  end
  # or
  # def self.localized_statuses
  #   Post.statuses.keys.map {|k| [k, Post.human_attribute_name("post.status.#{k}")] }
  # end
end
activerecord:
  attributes:
    post/status:
      submitted: "承認待ち"
      approved: "承認済み"
      rejected: "取り消し"

expected.each do |inputs, o_key|
it "`#{inputs.first}` and value `#{inputs.last}` returns output `#{o_key}`" do
c_key, p_key = inputs
field = select_with_options(persisted[p_key], collection: collections[c_key])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [87/80]

}

expected.each do |inputs, o_key|
it "`#{inputs.first}` and value `#{inputs.last}` returns output `#{o_key}`" do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [86/80]

[ :hash_sym, :integer ] => nil,
[ :hash_nil, nil ] => :no_status,
[ :hash_nil, :string ] => :approved,
[ :hash_nil, :integer ] => nil,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SymbolArray: Use %i or %I for an array of symbols.
Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.

[ :hash_sym, :string ] => :approved,
[ :hash_sym, :integer ] => nil,
[ :hash_nil, nil ] => :no_status,
[ :hash_nil, :string ] => :approved,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SymbolArray: Use %i or %I for an array of symbols.
Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.

[ :hash_sym, nil ] => nil,
[ :hash_sym, :string ] => :approved,
[ :hash_sym, :integer ] => nil,
[ :hash_nil, nil ] => :no_status,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.

[ :arrays_sym, :integer ] => nil,
[ :arrays_nil, :nil ] => :no_status,
[ :arrays_nil, :string ] => :approved,
[ :arrays_nil, :integer ] => nil,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SymbolArray: Use %i or %I for an array of symbols.
Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.

[ :arrays_sym, :string ] => :approved,
[ :arrays_sym, :integer ] => nil,
[ :arrays_nil, :nil ] => :no_status,
[ :arrays_nil, :string ] => :approved,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SymbolArray: Use %i or %I for an array of symbols.
Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.

[ :arrays_sym, nil ] => nil,
[ :arrays_sym, :string ] => :approved,
[ :arrays_sym, :integer ] => nil,
[ :arrays_nil, :nil ] => :no_status,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SymbolArray: Use %i or %I for an array of symbols.
Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.

[ :arrays_str, :integer ] => nil,
[ :arrays_sym, nil ] => nil,
[ :arrays_sym, :string ] => :approved,
[ :arrays_sym, :integer ] => nil,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SymbolArray: Use %i or %I for an array of symbols.
Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.

[ :arrays_str, :string ] => :approved,
[ :arrays_str, :integer ] => nil,
[ :arrays_sym, nil ] => nil,
[ :arrays_sym, :string ] => :approved,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/SymbolArray: Use %i or %I for an array of symbols.
Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.


describe "#label_data" do
it "returns nil without options" do
field = Administrate::Field::Select.new(:select, persisted[:string], :show)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [81/80]


expected.each do |in_key, out_key|
it "`#{in_key}` returns collection `#{out_key}`" do
field = select_with_options(persisted[:string], collection: collections[in_key])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [90/80]

:arrays_nil => :arrays_nil,
:hash_str => :arrays_str,
:hash_sym => :arrays_sym,
:hash_nil => :arrays_nil

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.
Style/TrailingCommaInHashLiteral: Put a comma after the last item of a multiline hash.

:arrays_sym => :arrays_sym,
:arrays_nil => :arrays_nil,
:hash_str => :arrays_str,
:hash_sym => :arrays_sym,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

:arrays_str => :arrays_str,
:arrays_sym => :arrays_sym,
:arrays_nil => :arrays_nil,
:hash_str => :arrays_str,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/HashSyntax: Use the new Ruby 1.9 hash syntax.

arrays_nil: [ ['No status', nil], ['Approved!', 'approved'] ],
hash_str: { 'Pending' => 'submitted', 'Approved!' => 'approved' },
hash_sym: { 'Pending' => :submitted, 'Approved!' => :approved },
hash_nil: { 'No status' => nil, 'Approved!' => 'approved' }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/TrailingCommaInHashLiteral: Put a comma after the last item of a multiline hash.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

arrays_sym: [ ['Pending', :submitted], ['Approved!', :approved] ],
arrays_nil: [ ['No status', nil], ['Approved!', 'approved'] ],
hash_str: { 'Pending' => 'submitted', 'Approved!' => 'approved' },
hash_sym: { 'Pending' => :submitted, 'Approved!' => :approved },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

arrays_str: [ ['Pending', 'submitted'], ['Approved!', 'approved'] ],
arrays_sym: [ ['Pending', :submitted], ['Approved!', :approved] ],
arrays_nil: [ ['No status', nil], ['Approved!', 'approved'] ],
hash_str: { 'Pending' => 'submitted', 'Approved!' => 'approved' },

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

array_int: [1, 2, 3],
arrays_str: [ ['Pending', 'submitted'], ['Approved!', 'approved'] ],
arrays_sym: [ ['Pending', :submitted], ['Approved!', :approved] ],
arrays_nil: [ ['No status', nil], ['Approved!', 'approved'] ],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

empty: [],
array_int: [1, 2, 3],
arrays_str: [ ['Pending', 'submitted'], ['Approved!', 'approved'] ],
arrays_sym: [ ['Pending', :submitted], ['Approved!', :approved] ],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceInsideArrayLiteralBrackets: Do not use space inside array brackets.
Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

array_int: [1,2,3],
arrays_str: [["Pending","submitted"],["Approved!","approved"]],
arrays_sym: [["Pending", :submitted],["Approved!",:approved]],
arrays_nil: [["No status",nil],["Approved!","approved"]],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAfterComma: Space missing after comma.

empty: [],
array_int: [1,2,3],
arrays_str: [["Pending","submitted"],["Approved!","approved"]],
arrays_sym: [["Pending", :submitted],["Approved!",:approved]],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAfterComma: Space missing after comma.

{
empty: [],
array_int: [1,2,3],
arrays_str: [["Pending","submitted"],["Approved!","approved"]],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAfterComma: Space missing after comma.

let(:collections) do
{
empty: [],
array_int: [1,2,3],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Layout/SpaceAfterComma: Space missing after comma.

when NilClass
nil
when Array
pair = collection.detect { |l,v| v.to_s == data.to_s } || []

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint/UnusedBlockArgument: Unused block argument - l. If it's necessary, use _ or _l as an argument name to indicate that it won't be used.
Layout/SpaceAfterComma: Space missing after comma.

@bjgaynor
Copy link

Any word on this? It would be really useful to have. I feel like Administrate has so much potential and then stuff like this doesn't get merged. Can I do something to fix it and get it merged? I'd be happy to update the syntax if that's the issue here.

@nickcharlton nickcharlton added feature new functionality that’s not yet implemented fields new fields, displaying and editing data labels Jan 2, 2020
@pablobm
Copy link
Collaborator

pablobm commented Apr 17, 2023

Closed due to lack of activity. Additionally, I am not sure about overloading collection. In order to make Administrate work with minimum surprises, I think collection should behave exactly like options_for_select. I have done this at #2348, which also implements enums.

However this means that collection would not be able to provide groups. I think this is fine and instead there should be a separate option for grouped options. Eg: grouped_options, which is the name it receives in the documentation of grouped_options_for_select.

@pablobm pablobm closed this Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature new functionality that’s not yet implemented fields new fields, displaying and editing data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants